import { Component, OnInit, Input } from '@angular/core'
import {
  IChargeBack,
  IChargeBackOperateReq,
  EChargeBackTransCode,
  EChargeBackProcessType,
} from '@shared/interfaces/operation'
import { NzMessageService } from 'ng-zorro-antd'
import { Router, ActivatedRoute } from '@angular/router'
import { OperationsService } from 'app/routes/operations/operations-abstract.service'
import { ChargeBackDataService } from '../../charge-back.data.service'
import { OperateBaseAbstractClass } from '../operate-base.abstract'
import { I18NService } from '@core'
import { Observable } from 'rxjs'
import { IGeneralResponse } from '@shared/interfaces/response'

@Component({
  selector: 'app-second-chargeback-operate',
  templateUrl: './second-chargeback-operate.component.html',
})
export class SecondChargebackOperateComponent extends OperateBaseAbstractClass implements OnInit {
  @Input() disputeNum: string
  @Input() chargeBackDetail: IChargeBack
  @Input() transCode: EChargeBackTransCode
  processType: EChargeBackProcessType = EChargeBackProcessType.Close
  EChargeBackProcessType = EChargeBackProcessType

  constructor(
    public msg: NzMessageService,
    public i18n: I18NService,
    private router: Router,
    private route: ActivatedRoute,
    private operateService: OperationsService,
    private chargeBackDataService: ChargeBackDataService,
  ) {
    super(msg, i18n)
  }

  ngOnInit() {}

  cancel(): void {
    this.router.navigate(['..'], { relativeTo: this.route })
  }

  submit(): void {
    const operation$: Observable<IGeneralResponse> = this.genOperation()
    operation$.subscribe(
      result => {
        this.router.navigate(['..'], { relativeTo: this.route })
      },
      err => {
        // 暂时隐去前端报错
        // this.msg.error(this.trans('GENERAL.NOTIFICATION.OPERATION_FAIL'))
      },
    )
  }

  genOperation(): Observable<IGeneralResponse> {
    switch (this.processType) {
      case EChargeBackProcessType.Close:
        return this.closeOperate()
      case EChargeBackProcessType.MerchantDebit:
        return this.debitOperate()
      case EChargeBackProcessType.Arbitrate:
        return this.arbitrateOperate()
    }
  }

  closeOperate(): Observable<IGeneralResponse> {
    const { remark } = this.chargeBackDataService.operateBody
    const isValid = this.checkValidAndShowMsg({ remark })
    if (!isValid) {
      return
    }
    const { disputeNum } = this
    const req: IChargeBackOperateReq = {
      disputeNum,
      remark,
      processType: EChargeBackProcessType.Close,
    }
    return this.operateService.handleChargeBack(req, this.transCode)
  }

  debitOperate(): Observable<IGeneralResponse> {
    const { remark, amt } = this.chargeBackDataService.operateBody
    const isValid = this.checkValidAndShowMsg({ remark })
    if (!isValid) {
      return
    }
    const { disputeNum } = this
    const req: IChargeBackOperateReq = {
      disputeNum,
      remark,
      transAmt: amt,
      processType: EChargeBackProcessType.MerchantDebit,
    }
    return this.operateService.handleChargeBack(req, this.transCode)
  }

  arbitrateOperate(): Observable<IGeneralResponse> {
    const { remark } = this.chargeBackDataService.operateBody
    const isValid = this.checkValidAndShowMsg({ remark })
    if (!isValid) {
      return
    }
    const { disputeNum } = this
    const req: IChargeBackOperateReq = {
      disputeNum,
      remark,
      processType: EChargeBackProcessType.Arbitrate,
    }
    return this.operateService.handleChargeBack(req, this.transCode)
  }
}
